﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XCharts;
using LitJson;
using Script.UI;
using MTUnity.Utils;
using Script.UI.Component;
using System;

public class UI_Check : UIUpdateBase
{
    public Text Txt_CurrentCheckInPlan;
    public Text Txt_CurrentCheckInCompleted;
    
    public LineChart LineChart_CheckRecord;
    public Text Txt_RefreshTime;


    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        JsonData json = new JsonData();

        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.SortingLine.Current_SortingCheck , UpdateCurrentSortingCheck },
            {JsonFildName.SortingLine.Record_SortingCheck , UpdateRecordSortingCheck },
            {JsonFildName.SortingLine.CheckRefreshTime , UpdateRefreshTime },
        };
    }
	#endregion

	#region UpdateInterface
    public void UpdateCurrentSortingCheck(JsonData jsonData)
    {
        if (jsonData.IsObject)
        {
            UpdateCurrentSortingCheck(
                JsonUtils.ToInt(jsonData[JsonFildName.SortingLine.SortingCheckInPlan]),
                JsonUtils.ToInt(jsonData[JsonFildName.SortingLine.SortingCheckInCompleted]));
        }
    }

    public void UpdateRecordSortingCheck(JsonData jsonData)
    {
        List<KeyValuePair<int, int>> temp = new List<KeyValuePair<int, int>>();
        for (int i = 0; i < jsonData.Count; i++)
        {
            temp.Add(
                new KeyValuePair<int, int>(
                    JsonUtils.ToInt(jsonData[i][JsonFildName.SortingLine.SortingCheckInPlan]),
                    JsonUtils.ToInt(jsonData[i][JsonFildName.SortingLine.SortingCheckInCompleted])
                    )
                );
        }
        UpdateRecordSortingCheck(temp);
    }

    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }
    #endregion

    public void UpdateCurrentSortingCheck(int iSortingCheckInPlan, int iSortingCheckInCompleted)
    {
        //Txt_CurrentCheckInPlan.text = iSortingCheckInPlan.ToString();
        Txt_CurrentCheckInCompleted.text = iSortingCheckInCompleted.ToString();
    }

    public void UpdateRecordSortingCheck(List<KeyValuePair<int , int>> listData)
    {
        for (int i = 0; i < listData.Count; i++)
        {
            LineChart_CheckRecord.UpdateData(0, i, 1);
            LineChart_CheckRecord.UpdateData(1, i, 0);
            //LineChart_CheckRecord.UpdateData(0, i, (float)listData[i].Key);
            //LineChart_CheckRecord.UpdateData(1, i, (float)listData[i].Value);
        }
    }

    public void UpdateRefreshTime(long lRefreshTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lRefreshTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}", datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }
}